package ysEntry

import (
	"errors"
	"strconv"
	"strings"
)

var openidCharMap map[rune]byte

func init() {
	a := "ABCDEFGHJKMNPQRSTUVWXYZ23456789"
	b := "0123456789abcdefghijklmnopqrstu"
	m := make(map[rune]byte, len(a))
	for i, r := range a {
		if i < len(b) {
			m[r] = b[i]
		}
	}
	openidCharMap = m
}

// 将openid转换为int64
func OpenIdToInt64(openId string) (int64, error) {
	if len(openId) != 8 {
		return 0, errors.New("openId长度不正确")
	}

	var result strings.Builder
	result.Grow(len(openId))
	for _, r := range openId {
		result.WriteByte(openidCharMap[r])
	}
	s32 := result.String()

	return strconv.ParseInt(s32, 32, 64)
}

// 角色id转存档id
/*
func GeneratePlayerDbId(playerID string) (primitive.ObjectID, error) {
	if len(playerID) != 8 {
		return primitive.NilObjectID, errors.New("generatePlayerDbId string length must = 8")
	}
	var oid [12]byte
	copy(oid[:8], []byte(playerID))
	copy(oid[8:], []byte("0000"))

	//primitive.ObjectIDFromHex
	return oid, nil
}*/
